﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace UserInterface
{
    public class NetworkInterface
    {

        public static void Main(string[] args)
        {
            StreamWriter carWriter;
            StreamReader carReader;
            TcpClient tcp;
            try
            {
                tcp = new TcpClient();
                tcp.Connect("127.0.0.1", 9090);
                Console.WriteLine("Connection Established");
                carWriter = new StreamWriter(tcp.GetStream());
                carReader = new StreamReader(tcp.GetStream());
                string vehicleAddress = null;
                bool attemptConnection = true;
                while (attemptConnection)
                {
                    Console.Write("Enter password: ");
                    carWriter.WriteLine("AUTH: " + Console.ReadLine());
                    carWriter.Flush();
                    string response = carReader.ReadLine();
                    if (response != "FAIL")
                    {
                        attemptConnection = false;
                        vehicleAddress = response;
                    }
                }
                tcp.Close();

                tcp = new TcpClient();
                tcp.Connect(vehicleAddress, 9091);
                carWriter = new StreamWriter(tcp.GetStream());
                carReader = new StreamReader(tcp.GetStream());
                Console.WriteLine("Connected to car.");
                while (true)
                {
                    carWriter.WriteLine(Console.ReadLine());
                    carWriter.Flush();
                }
            }
            catch
            {
                Console.WriteLine("Network Error");
            }
        }
    }
}
